import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:http/http.dart' as http;
import '../Robot.dart';
import '../details.dart';
import '../pojo.dart';

const platformMethodChannel = MethodChannel('com.abc.robot01.flutter.qa');

///地图
///
class MapProvider extends ChangeNotifier {
  List<String> maps = [];
  List<String> points = [];

  Future<void> fetchAllMaps() async {
    try {
      final String result = await platformMethodChannel.invokeMethod(
        'get_all_maps',
      );
      print("result:" + result);
      maps = result.split("_");
      print("fetchData:" + maps[0]);
      notifyListeners(); // Update the state and notify listeners
    } on PlatformException catch (e) {
      print("Failed to get data: '${e.message}'.");
    }
  }

  String _qaTitle = "我是地图标题";

  String get qaTitle => _qaTitle;

  String _qaTitle2 = "我是地图对应内容";

  String get qaTitle2 => _qaTitle2;

  String ask = "我是问路播放内容";
  String arrive = "我是到达播放内容";
  bool ok1 = false;
  String start = "我是出发播放内容";

  ///自定义语音配置的当前输入内容
  final _keyowrds = [""];

  ///只在界面刷新之后调用
  void updateContent(int index, String value) {
    print(
      "AddQAProvider updateContent: " +
          index.toString() +
          ":" +
          value.toString(),
    );
    _keyowrds[index] = value;
  }

  void removeAt(int index) {
    _keyowrds.removeAt(index);
    notifyListeners();
  }

  void setOk1(bool a) {
    ok1 = a ;
    notifyListeners();
  }

  List<String> get keyowrds => _keyowrds;

  String _selectedMap = "请选择";

  String get selectedMap => _selectedMap;

  void setSelectedMap(String name) async {
    _selectedMap = name;
    final String result = await platformMethodChannel.invokeMethod(
        'get_points',{"name":name}
    );
    print("get_points result:" + result);
    points = result.split("_");
    notifyListeners();
  }

  String _selectedPoint = "请选择";

  String get selectedPoint => _selectedPoint;

  void setSelectedPoint(String name) async {
    _selectedPoint = name;
    notifyListeners();
  }

  void addSize() {
    _keyowrds.add("");
    notifyListeners();
  }

  final String name = "name";

  Future<bool> login() async {
    print("login start");
    var url = Uri.https(host, 'robot/login');
    var response = await http.post(url, body: {'sn': Robot.sn});
    var a = ApiResponse<LoginBean>.fromJson(
      jsonDecode(response.body),
      (data) => LoginBean.fromJson(data),
    ); // 传递一个从 JSON 解析 LoginBean 的方法);

    print(a.data.toString());
    if (a.data?.userId == null) {
      print('请扫码');
      return false;
    }
    Robot.userId = a.data!.userId!;
    Robot.companyId = a.data!.companyId!;
    Robot.accessToken = a.data!.accessToken!;
    print("Robot.accessToken:" + Robot.accessToken);
    print("login end");
    return true;
  }
}
